package com.qiahao.socket;

import com.qiahao.constant.Cons;
import com.qiahao.service.QuestionService;
import com.qiahao.service.impl.QuestionServiceImpl;
import com.qiahao.util.SpringContextUtil;
import org.dom4j.*;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.websocket.*;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;

/**
 * Created by Administrator on 2018/3/29 0029.
 */
@Service
@ClientEndpoint
public class Client{

    @Autowired
    QuestionService questionService;



    Session session;
    ApplicationContext applicationContext;
    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
        try {
            session.getBasicRemote().sendText("ceshi");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("questionService: " + questionService);

    }

    @OnClose
    public void onClose() throws IOException {
        WebSocketContainer container = ContainerProvider.getWebSocketContainer(); // 获取WebSocket连接器，其中具体实现可以参照websocket-api.jar的源码,Class.forName("org.apache.tomcat.websocket.WsWebSocketContainer");
        Session session = null; // 连接会话
        try {
            session = container.connectToServer(Client.class, new URI(Cons.socketUrl));
        } catch (DeploymentException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        System.out.println("onClose to endpoint: " + session.getBasicRemote());
    }

    @OnMessage
    public void onMessage(String message) {
        Long startTime  =  System.currentTimeMillis();
//        message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>  <root>"+message+"</root>";
        message = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <root> "+message+"</root>";
        try {
            System.out.println("message-----------------------"+message);
            Document dom=DocumentHelper.parseText(message);
            Element root=dom.getRootElement();
            String msgType=root.element("MsgType").getText();
            String ChNum=root.element("ChNum").getText();
            String RecordName=root.element("RecordName").getText();




            questionService = (QuestionService) SpringContextUtil.getApplicationContext().getBean("questionServiceImpl");
            String newfilename ="C:\\" +"share\\"+RecordName.substring(3,RecordName.length());
//            questionService = new QuestionServiceImpl();

            String result = questionService.getQuestionResult(new File(newfilename), Long.parseLong(msgType));

            StringBuilder returnmsg = new StringBuilder();
            returnmsg.append("<MsgType>");
            returnmsg.append(7900);
            returnmsg.append("</MsgType>");
            returnmsg.append("<ChNum>");
            returnmsg.append(ChNum);
            returnmsg.append("</ChNum>");
            returnmsg.append("<Result>");
            returnmsg.append(result);
            returnmsg.append("</Result>");
            System.out.print("result"+returnmsg);
            try {
                session.getBasicRemote().sendText(returnmsg.toString());
                Long endTime  =  System.currentTimeMillis();
                Long time  = endTime-startTime;
                System.out.print("耗时:"+time);

            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        System.out.println(message);
    }



    @OnError
    public void onError(Throwable t) {
        System.out.println("Connected error: " + t.toString());
        t.printStackTrace();
    }


}
